package com.laizhenghua.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laizhenghua.hotel.utils.PageUtils;
import com.laizhenghua.hotel.utils.Query;

import com.laizhenghua.hotel.dao.UsersDao;
import com.laizhenghua.hotel.entity.UsersEntity;
import com.laizhenghua.hotel.service.UsersService;


@Service("usersService")
public class UsersServiceImpl extends ServiceImpl<UsersDao, UsersEntity> implements UsersService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UsersEntity> page = this.page(
                new Query<UsersEntity>().getPage(params),
                new QueryWrapper<UsersEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public UsersEntity userLogin(String username, String password) {
        UsersEntity usersEntity = this.baseMapper.selectOne(new QueryWrapper<UsersEntity>()
                .eq("username",username)
                .eq("password",password));
        return usersEntity;
    }

    @Override
    public boolean updateUserGrade(Map<String, String> map) {
        // UPDATE `hp_hotel`.`tb_users` SET grade = ?,SET TYPE = ? WHERE id = ?;
        boolean update = this.update(new UpdateWrapper<UsersEntity>()
                .set("grade", map.get("grade"))
                .set("type", map.get("type"))
                .eq("id", map.get("id")));
        return update;
    }

    @Override
    public UsersEntity getOneByTelAndUsername(String username, Object tel) {
        UsersEntity usersEntity = this.getOne(new QueryWrapper<UsersEntity>()
                .eq("username", username)
                .eq("tel", tel));
        return usersEntity;
    }

}